/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client;

import gjt.DrawnRectangle;
import gjt.ThreeDRectangle;
import gjt.Util;
import edu.idp.client.draw.AdvancedDrawPanel;

import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;

/**
 *
 * @author Kristopher T Babic
 */
class ColorPanel extends Panel implements MouseListener, ActionListener {

    private int width;
    private ThreeDRectangle colors[];
    private DrawnRectangle current;
    private AdvancedDrawPanel drawingPanel;
    private Color curColor;
    private Color colorList[];
//	private ThreeDRectangle temp;
    //	private ThreeDRectangle temp2;
    //	private ThreeDRectangle temp3;
    private Image colorButton = null;
    private JButton colorSelectionButton = null;

    public ColorPanel(AdvancedDrawPanel drawingPanel, Applet myApp) {
        this.setLayout(null);
        this.drawingPanel = drawingPanel;
//		width = toolPanel.getPreferredSize().width + drawPanel.getPreferredSize().width;
//		setLayout(new BorderLayout());
        curColor = new Color(0, 0, 0);
        drawingPanel.setColor(curColor);


        colorList = new Color[10];
        colors = new ThreeDRectangle[10];

        //for(int i = 0; i < 10; i++)
        //	color[i] = new Color();
        colorList[0] = Color.black;
        colorList[1] = Color.blue;
        colorList[2] = Color.cyan;
        colorList[3] = Color.darkGray;
        colorList[4] = Color.gray;
        colorList[5] = Color.white;
        colorList[6] = Color.green;
        colorList[7] = Color.red;
        colorList[8] = Color.orange;
        colorList[9] = Color.yellow;

        for (int i = 0; i < 5; i++)
            colors[i] = new ThreeDRectangle(this, 100 + 17 * i, 9, 15, 15);

        for (int i = 5; i < 10; i++)
            colors[i] = new ThreeDRectangle(this, 100 + 17 * (i - 5), 26, 15, 15);

//		temp = new ThreeDRectangle(this, 250, 20, 20, 20);
//		temp2 = new ThreeDRectangle(this, 300, 20, 20, 20);
//    temp3 = new ThreeDRectangle(this, 200, 20, 20, 20);
        URL base = myApp.getCodeBase();

        colorButton = myApp.getImage(base, "./gifs/palette.gif");

        Util.waitForImage(this, colorButton);

        //    if (colorButton == null)
//      System.out.println("\n\nnull image\n");

        ImageIcon ic = new ImageIcon(colorButton);

        colorSelectionButton = new JButton(ic);
        colorSelectionButton.setBounds(105 + 17 * 5, 9, 32, 32);
        colorSelectionButton.addActionListener(this);
//    colorSelectionButton.setSize(30,30);
        colorSelectionButton.setBackground(new Color(12632256));
        colorSelectionButton.setToolTipText("Select Color From Pallete");
        add(colorSelectionButton);
//    Toolkit tk = Toolkit.getDefaultToolkit();

        current = new DrawnRectangle(this, 35, 10, 30, 30);
        current.setLineColor(Color.black);

        addMouseListener(this);
    }

    public void paint(Graphics g) {
        super.paint(g);
        for (int i = 0; i < 10; i++) {
            colors[i].paint();
            colors[i].fill(colorList[i]);
        }

        current.paint();
        current.fill(curColor);
//    g.drawImage(colorButton, 300, 9, 30, 30, this);
//		temp.paint();
//		temp2.paint();
//    temp3.paint();
    }

    public Dimension getPreferredSize() {
        return new Dimension(600, 50);
    }

    public Dimension getMinimumSize() {
        return new Dimension(600, 50);
    }

    public void mouseClicked(MouseEvent event) {
//System.out.println("mouse click");
//		mousePressed(event);
    }

    public void mouseReleased(MouseEvent event) {
    }

    public void mouseEntered(MouseEvent event) {
    }

    public void mouseExited(MouseEvent event) {
    }

    public void mousePressed(MouseEvent event) {
//System.out.println("mouse press");
        int x = event.getPoint().x;
        int y = event.getPoint().y;

        for (int i = 0; i < 10; i++) {
            if (colors[i].contains(x, y)) {
                curColor = colors[i].getFillColor();
                current.fill(curColor);
                drawingPanel.setColor(curColor);
                break;
            }
        }
/*
		if (temp.contains(x,y))
			drawingPanel.clear();
		else if (temp2.contains(x,y))
			drawingPanel.restoreAll();
    else if (temp3.contains(x,y)) {
      Color newColor = JColorChooser.showDialog(this, "Select Drawing Color", curColor);
      if (newColor != null){
        curColor = newColor;
        current.fill(curColor);
        drawingPanel.setColor(curColor);
      }
    }*/
    }

    /**
     * *******************************
     * Action Listener
     * ********************************
     */
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == colorSelectionButton) {
            Color newColor = JColorChooser.showDialog(this, "Select Drawing Color", curColor);
            if (newColor != null) {
                curColor = newColor;
                current.fill(curColor);
                drawingPanel.setColor(curColor);
            }

        }
    }

}
